package cn.tedu.online.examination.system.ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.Serializable;

/**
 * @author JokerQ
 * @date 2020/6/15 - 14:07
 */
public class RegisterFrame extends JFrame implements Serializable {

    private static final long serialVersionUID = -6271438370718724248L;

    private ClientContext clientContext;

    public RegisterFrame(){
        into();
    }

    private void into() {
        setTitle("注册界面");
        setSize(300, 250);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        add(createContenPane());
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                clientContext.exit(RegisterFrame.this);
            }
        });
    }

    private JPanel createContenPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.setBorder(new EmptyBorder(8, 8, 8, 8));
        pane.add(BorderLayout.NORTH, new JLabel("注册界面", JLabel.CENTER));
        pane.add(BorderLayout.CENTER, createCenterPane());
        pane.add(BorderLayout.SOUTH, createButtonPane());
        return pane;
    }

    private JPanel createCenterPane() {
        JPanel pane = new JPanel(new GridLayout(5, 1, 10, 10));
        pane.setBorder(new EmptyBorder(10, 0, 0, 0));
        pane.add(createIdPane());
        pane.add(createPwdPane());
        pane.add(createNamePane());
        pane.add(createEmailPane());
        pane.add(createPhonePane());
        return pane;
    }

    private JPanel createIdPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.WEST, new JLabel("编号：   "));
        JTextField idField = new JTextField();
        pane.add(BorderLayout.CENTER, idField);
        return pane;

    }

    private JPanel createPwdPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.WEST, new JLabel("密码：   "));
        JPasswordField pwd = new JPasswordField();
        pwd.enableInputMethods(true);
        pane.add(BorderLayout.CENTER, pwd);
        return pane;

    }

    private JPanel createNamePane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.WEST, new JLabel("姓名：   "));
        JTextField name = new JTextField();
        pane.add(BorderLayout.CENTER, name);
        return pane;
    }

    private JPanel createEmailPane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.WEST, new JLabel("Email： "));
        JTextField email = new JTextField();
        pane.add(BorderLayout.CENTER, email);
        return pane;
    }

    private JPanel createPhonePane() {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(BorderLayout.WEST, new JLabel("电话：   "));
        JTextField phone = new JTextField();
        pane.add(BorderLayout.CENTER, phone);
        return pane;
    }

    private JPanel createButtonPane() {
        JPanel pane = new JPanel(new FlowLayout());
        JButton register = new JButton("注册");
        JButton cancel = new JButton("返回");
        pane.add(register);
        pane.add(cancel);
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clientContext.register();
            }
        });
        return pane;
    }

    public void setClientContext(ClientContext clientContext) {
        this.clientContext = clientContext;
    }
}
